package com.guangzhi.weijianzhi.activity;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.guangzhi.weijianzhi.R;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.umeng.analytics.MobclickAgent;

/**
 * @author 忘记密码
 * 
 */
public class ForgetPasswordActivity extends BaseActivity implements OnClickListener {

	private EditText  newPasswordEdt, okNewPasswordEdt,
			verificationEdt,phoneEdt;

	private Button saveBtn;
	private Button verificationBtn;
	private TimeCount time; // 倒计时内部类

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.forget_passwrd_layout);
		initView();
		onclicklistener();
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
	}
	

	public void initView() {
		initTitle();
		saveBtn = (Button) this.findViewById(R.id.save_btn);
		titleTextV.setText("忘记密码");
		verificationBtn = (Button) this.findViewById(R.id.get_yanzheng_btn);
		newPasswordEdt = (EditText) this.findViewById(R.id.password_edt);
		okNewPasswordEdt = (EditText) this.findViewById(R.id.ok_password_edt);
		verificationEdt = (EditText) this.findViewById(R.id.yanzheng_edt);
		phoneEdt =  (EditText) this.findViewById(R.id.phone_edt);
		time = new TimeCount(60000, 1000);
	}

	public void onclicklistener() {
		goBack.setOnClickListener(this);
		saveBtn.setOnClickListener(this);
		verificationBtn.setOnClickListener(this);
	
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.goBack:
			ForgetPasswordActivity.this.finish();
			break;

		case R.id.save_btn:
			if (phoneEdt.getText()==null && "".equals(phoneEdt.getText().toString().trim())) {
				ToastUtil("请输入手机号");
				return;
			}
			if(newPasswordEdt.getText()!=null && "".equals(newPasswordEdt.getText().toString().trim())){
				ToastUtil("请填写新密码");
				return;
			}
			if(newPasswordEdt.getText()!=null && "".equals(newPasswordEdt.getText().toString().trim())){
				ToastUtil("请确认新密码");
				return;
			}
			if(!newPasswordEdt.getText().toString().trim().equals(newPasswordEdt.getText().toString().trim())){
				ToastUtil("两次输入密码不一致");
				return;
			}
			if(verificationEdt.getText()!=null && "".equals(verificationEdt.getText().toString().trim())){
				ToastUtil("请输入验证码");
				return;
			}
			else{
				forgetPassword();
			}
			break;

		case R.id.get_yanzheng_btn:
			if(phoneEdt.getText().toString().trim().equals("")){
				ToastUtil("请输入您的手机号码");
			}else {
				verificationBtn.setBackgroundResource(R.drawable.login_right_pre_bg);
				verificationBtn.setClickable(false);
				saveBtn.setClickable(false);
				sendVerifica();
			}
			break;
		}
	}
	public void ToastUtil(String str) {
		Toast.makeText(ForgetPasswordActivity.this, str, Toast.LENGTH_SHORT)
				.show();
	}

	/**
	 * 获取验证码
	 */
	public void sendVerifica() {
//		new Handler().postDelayed(new Runnable() {
//			@SuppressWarnings("static-access")
//			public void run() {
//				RequestParams rParam = new RequestParams();
//				rParam.put("tel", phoneEdt.getText().toString().trim());
//				RequestFactory.getInstance().post(
//						RequestFactory.RUL_VERIFICATION, rParam,
//						new AsyncHttpResponseHandler() {
//
//							@Override
//							public void onFailure(int arg0, Header[] arg1, byte[] arg2, Throwable arg3) {
//								
//							}
//
//							@Override
//							public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
//								try {
//									JSONObject json = new JSONObject(new String(arg2));
//									String code = json.getString("msg");
//									if ("ok".equals(code)) {
//										time.start();// 倒计时开始；
//									} else {
//										Toast.makeText(
//												ForgetPasswordActivity.this,
//												code, Toast.LENGTH_SHORT)
//												.show();
//										verificationBtn.setText("获取验证码");
//										verificationBtn.setClickable(true);
//										verificationBtn.setBackgroundResource(R.drawable.login_right_bg);
//										
//									}
//								} catch (JSONException e) {
//									e.printStackTrace();
//								}
//								
//							}
//						});
//			}
//		}, 50);

	}

	/**
	 * 忘记密码
	 */
	public void forgetPassword() {
//		new Handler().postDelayed(new Runnable() {
//			@SuppressWarnings("static-access")
//			public void run() {
//				RequestParams rParam = new RequestParams();
//				rParam.put("username", phoneEdt.getText().toString().trim());
//				rParam.put("pincode", verificationEdt.getText().toString()
//						.trim());
//				rParam.put("newpassword", newPasswordEdt.getText().toString()
//						.trim());
//				RequestFactory.getInstance().post(
//						RequestFactory.URL_FORGET_PASSWORD, rParam,
//						new AsyncHttpResponseHandler() {
//
//							@Override
//							public void onFailure(int arg0, Header[] arg1, byte[] arg2, Throwable arg3) {
//								
//							}
//
//							@Override
//							public void onSuccess(int arg0, Header[] arg1, byte[] response) {
//								try {
//									JSONObject json = new JSONObject(new String(response));
//									String code = json.getString("msg");
//									if ("ok".equals(code)) {
//										Toast.makeText(
//												ForgetPasswordActivity.this,
//												"修改成功", Toast.LENGTH_SHORT)
//												.show();
//										ForgetPasswordActivity.this.finish();
//									} else {
//										Toast.makeText(
//												ForgetPasswordActivity.this,
//												code, Toast.LENGTH_SHORT)
//												.show();
//									}
//								} catch (JSONException e) {
//									e.printStackTrace();
//								}
//								
//							}
//						});
//			}
//		}, 50);

	}

	/**
	 * @author Administrator 自定义一个倒计时的内部类；
	 */
	class TimeCount extends CountDownTimer {
		public TimeCount(long millisInFuture, long countDownInterval) {
			super(millisInFuture, countDownInterval);// 参数依次为总时长,和计时的时间间隔
		}

		// 当时间到的时候；
		public void onFinish() {
			// TODO Auto-generated method stub
			verificationBtn.setText("获取验证码");
			verificationBtn.setClickable(true);
			verificationBtn.setBackgroundResource(R.drawable.login_right_bg);
		}

		@Override
		public void onTick(long millisUntilFinished) {
			// TODO Auto-generated method stub
			
			verificationBtn.setText(millisUntilFinished / 1000 + "秒");
		}
	}

}
